//
//  MoWebNode.swift
//  dycommon
//
//  Created by zhuxietong on 2021/2/3.
//

import Foundation
import Eelay
import WebKit
import AsyncDisplayKit



open class MoWebCellNode : JoCellNode,TagsRenderDelegate{
    
    open var node = ASDisplayNode { () -> UIView in
        let webView = EeWebView()
        
        webView.webView.isOpaque = false
        webView.webView.backgroundColor = ._background
       
        return webView
    }
    public var eeWeb:EeWebView{
        return node.view as! EeWebView
    }
    public var changeHeight:(_:CGFloat)->Void = {_ in}

   
    
    
    open override func addLayoutRules() {
        eeWeb.delegete = self
        eeWeb.webView.backgroundColor = ._background
        eeWeb.changTheHeight = { [weak self]
            h in
            print("changTheHeightchangTheHeightchangTheHeight",h)
            self?.height = h
        }
    }
    
    
    public var height:CGFloat = Swidth{
        didSet{
          

            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.2)
            UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: 7)!)
//                    self.alpha = 0
            updateWebHeight()
            changeHeight(height)
            UIView.commitAnimations()

//            updateWebHeight()
//            changeHeight(height)
            
            //            indexPathUpdate()
        }
    }
    open func updateWebHeight(){
        
        setNeedsLayout()
        layoutIfNeeded()
//        if sec == -1 || row == -1{
//            active(ID: EeWebView.updateWebHeightKey, object: webModel)
//            return
//        }
//        if let ctr = mo.controller as? ASListController{
//
//
//            ctr.tableNode.performBatchUpdates({
//                let path = IndexPath(row: 0, section: 0)
//                ctr.tableNode.reloadRows(at: [path], with: .none)
//            }, completion: nil)
//        }
//
//        if let ctr = mo.controller as? ASColListController{
//            ctr.collectionNode.performBatchUpdates({
//                let path = IndexPath(item: 0, section: 0)
//                ctr.collectionNode.reloadItems(at: [path])
//            }, completion: nil)
//        }
        
    }
    
    open func replace(_ tag: String) -> String {
        return ""
    }

    
    
    override open func specGetter() -> _Spec {
        let h = eeWeb.webHeight
        
        return .inset([h,Sheight], .zero, child: node[h])
    }
    
    deinit {
    }
    
}

open class MoDisplayWebNode : MoDisplayNode,TagsRenderDelegate{
    
    open var node = ASDisplayNode { () -> UIView in
        let webView = EeWebView()
        
        webView.webView.isOpaque = false
        webView.webView.backgroundColor = ._background
       
        return webView
    }
    public var eeWeb:EeWebView{
        return node.view as! EeWebView
    }
    public var changeHeight:(_:CGFloat)->Void = {_ in}

   
    
    
    open override func addLayoutRules() {
        eeWeb.delegete = self
        eeWeb.webView.backgroundColor = ._background
        eeWeb.changTheHeight = { [weak self]
            h in
            print("changTheHeightchangTheHeightchangTheHeight",h)
            self?.height = h
        }
    }
    
    
    public var height:CGFloat = Swidth{
        didSet{
          

            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.2)
            UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: 7)!)
//                    self.alpha = 0
            updateWebHeight()
            changeHeight(height)
            UIView.commitAnimations()

//            updateWebHeight()
//            changeHeight(height)
            
            //            indexPathUpdate()
        }
    }
    open func updateWebHeight(){
        
        setNeedsLayout()
        layoutIfNeeded()
//        if sec == -1 || row == -1{
//            active(ID: EeWebView.updateWebHeightKey, object: webModel)
//            return
//        }
//        if let ctr = mo.controller as? ASListController{
//
//
//            ctr.tableNode.performBatchUpdates({
//                let path = IndexPath(row: 0, section: 0)
//                ctr.tableNode.reloadRows(at: [path], with: .none)
//            }, completion: nil)
//        }
//
//        if let ctr = mo.controller as? ASColListController{
//            ctr.collectionNode.performBatchUpdates({
//                let path = IndexPath(item: 0, section: 0)
//                ctr.collectionNode.reloadItems(at: [path])
//            }, completion: nil)
//        }
        
    }
    
    open func replace(_ tag: String) -> String {
        return ""
    }

    
    
    override open func specGetter() -> _Spec {
        let h = eeWeb.webHeight
        
        return .inset([h,Sheight], .zero, child: node[h])
    }
    
    deinit {
    }
    
}
